﻿using System;
using System.Collections.Generic;

namespace Hydrogen.MathExt.Expression
{
    public static class ExpressionHelper
    {
        public const string LEFT_PARENTHESIS = "(";
        public const string RIGHT_PARENTHESIS = ")";
        public const string COMMA = ",";

        #region 常量
        private static readonly Dictionary<string, double> _constants =
            new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase)
        {
            {"pi",  System.Math.PI},
            {"e",  System.Math.E}
        };

        /// <summary>
        /// 获取常量
        /// </summary>
        public static double GetConstants(string key)
        {
            if (_constants.TryGetValue(key, out var result)) return result;
            return 0;
        }
        /// <summary>
        /// 是否是常量
        /// </summary>
        public static bool IsConstants(string key) => _constants.ContainsKey(key);

        public static bool TryGetConstants(string key, out double result) => _constants.TryGetValue(key, out result);

        public static void RegistConstants(string key, double value, bool force = false)
        {
            if (string.IsNullOrEmpty(key)) return;

            var b = IsConstants(key);
            if (b && !force)
            {
                throw new ArgumentException($"已经注册过常量\"{key}\"={GetConstants(key)}，不建议覆盖已有的常量。如果想要覆盖已有的常量，请传入参数<force=true>来强制覆盖");
            }

            _constants[key] = value;
        }
        public static List<string> GetSupportedConstants() => new List<string>(_constants.Keys);
        #endregion
    }
}
